﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Text;

using SimpleText.Framework;

public partial class FindUnusedTextsPage : SimpleTextPage 
{
	
  protected void Page_Load(object sender, EventArgs e)
  {
		InitPage();

		if (CurrentModel == null)
		{
			Response.Redirect("projects.aspx?msg=noprojectselected");
		}
	}

	protected void ContinueStep1Clicked(object sender, EventArgs e)
	{
		if (!Directory.Exists(PathTxt.Value))
		{
			Step1ErrorPlh.Visible = true;
			Step1ErrorLit.Text = "<div class=\"warning\">Invalid path specified</div>";
			return;
		}

		string[] extensions = ExtensionsTxt.Value.Split(',');
		for (int i = 0; i < extensions.Length; i++)
		{
			extensions[i] = "." + extensions[i];
		}

		List<TextGroupFoundByWizard> foundGroups = new List<TextGroupFoundByWizard>();
		foundGroups = ExtractTextsFromFiles(PathTxt.Value, extensions, foundGroups, PathTxt.Value, RegexTxt.Value);

		//persist to viewstate (we need it again in the next wizard step)
		ViewState["ExtractedFromPath"] = PathTxt.Value;

		//make combined group with all definitions (regardles of doublets)
		IDictionary<string, bool> TextDefinitionNamesFoundInFiles = new Dictionary<string, bool>();
		foreach (TextGroupFoundByWizard foundGroup in foundGroups)
		{
			if (foundGroup.TextDefinitions.Count != 0)
			{
				foreach (TextDefinitionFoundByWizard foundTextDef in foundGroup.TextDefinitions)
				{
					TextDefinitionNamesFoundInFiles[foundTextDef.TextDefinitionName] = true;
				}
			}
		}
		//find unused
		List<string> UnusedDefinitionNames = new List<string>();
		foreach (string definitionName in CurrentModel.TextDefinitions.Keys)
		{
			if ( ! TextDefinitionNamesFoundInFiles.ContainsKey(definitionName))
			{
				UnusedDefinitionNames.Add(definitionName);
			}
		}

		ViewState["UnusedDefinitionNames"] = UnusedDefinitionNames;

		NumberOfUnusedTexts.Text = UnusedDefinitionNames.Count.ToString();

		string textRegExString = RegexTxt.Value;//"Txt.Get[a-z]*\\(\\\"(?<textmatch>([a-z]*))\\\"\\)";
		string path = PathTxt.Value;
		string extensionsCommaSep = ExtensionsTxt.Value;
		RegexLit.Text = Server.HtmlEncode(textRegExString);
		PathLit.Text = Server.HtmlEncode(path);
		ExtensionsLit.Text = Server.HtmlEncode(extensionsCommaSep);

		FoundUnusedTextDefinitionNameRpt.DataSource = UnusedDefinitionNames;
		FoundUnusedTextDefinitionNameRpt.DataBind();
		
		//set interface state
		Step1Plh.Visible = false;
		Step2Plh.Visible = true;
		SuccessPlh.Visible = false;
	}


	protected void ContinueStep2Clicked(object sender, EventArgs e)
	{
		List<string> UnusedDefinitionNames = (List<string>)ViewState["UnusedDefinitionNames"];
		string extractedFromPath = (string)ViewState["ExtractedFromPath"];

		foreach (string definitionName in UnusedDefinitionNames)
		{
			CurrentModel.TextDefinitions.Remove(definitionName);
		}

		//Persist changed model
		SaveModel(CurrentModel, CurrentModel.LoadedFromXmlFilePath);

		//reload model
		Session["CurrentModel"] = null;
		
		//set interface state
		Step1Plh.Visible = false;
		Step2Plh.Visible = false;
		SuccessPlh.Visible = true;
	}
}
